package com.frxuexi.park2304.my

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.frxuexi.lib_base.entity.MyApplyBean
import com.frxuexi.lib_base.entity.ParkBeanItem
import com.frxuexi.lib_base.entity.PatrolBean
import com.frxuexi.lib_base.entity.RepairBean
import com.frxuexi.lib_base.entity.VisitBean
import com.frxuexi.lib_base.http.Const
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.RequestType
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.park2304.R
import com.frxuexi.park2304.databinding.ActivityMyApplyBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/MyApplyActivity")
@AndroidEntryPoint
class MyApplyActivity : BaseActivity<ActivityMyApplyBinding,MyIntent,MyViewModel>() {
    override val binding: ActivityMyApplyBinding by lazy { ActivityMyApplyBinding.inflate(layoutInflater) }
    override val vm: MyViewModel by viewModels<MyViewModel>()
    val myApplyAdapter by lazy { MyApplyAdapter() }
    val list= mutableListOf<MyApplyBean>()
    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.DEFAULT->{
                                val item = it.data as ParkBeanItem
                                myApplyAdapter.data.clear()
                                list.add(MyApplyBean("车位申请","申请${item.areaName} ${item.spaceNumber}车位",item.spaceStatus,"2025-08-29"))
                                myApplyAdapter.addData(list)
                                myApplyAdapter.notifyDataSetChanged()
                            }
                            RequestType.MYVISIT->{
                                val bean = it.data as VisitBean
                                myApplyAdapter.data.clear()
                                bean.forEach {
                                    list.add(MyApplyBean("来访申请","访客：${it.visitorName}(1人)|来访时间：${it.appointmentTime}",it.visitStatus,it.appointmentTime))
                                    myApplyAdapter.addData(list)
                                }
                                myApplyAdapter.notifyDataSetChanged()
                            }
                            RequestType.MYREPAIR->{
                                val bean = it.data as RepairBean
                                myApplyAdapter.data.clear()
                                bean.forEach {
                                    list.add(MyApplyBean("报修申请","${it.repairLocation} ${it.faultDescription}",it.processingStatus,it.repairTime))
                                    myApplyAdapter.addData(list)
                                }
                                myApplyAdapter.notifyDataSetChanged()
                            }
                            RequestType.MYPATROL->{
                                val bean = it.data as PatrolBean
                                myApplyAdapter.data.clear()
                                bean.forEach {
                                    list.add(MyApplyBean("巡更申请","${it.taskName}巡更申请",it.patrolStatus,it.patrolTime))
                                    myApplyAdapter.addData(list)
                                }
                                myApplyAdapter.notifyDataSetChanged()
                            }

                            else -> {}
                        }
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        binding.rvMy.apply {
            adapter=myApplyAdapter
            layoutManager=LinearLayoutManager(this@MyApplyActivity)
        }
        vm.sendIntent(MyIntent.getMyPark(SPUtils.getInstance().getInt(Const.UID)))
        vm.sendIntent(MyIntent.getMyVisit(SPUtils.getInstance().getString(Const.UNAME)))
        vm.sendIntent(MyIntent.getMyRepairs(SPUtils.getInstance().getString(Const.UNAME)))
        vm.sendIntent(MyIntent.getMyPatrols(SPUtils.getInstance().getInt(Const.UID)))

        binding.myParkCon.setOnClickListener {
            myApplyAdapter.apply {
                data.clear()
                addData(list.filter { it.type=="车位申请" })
                notifyDataSetChanged()
            }
        }
        binding.myVisitCon.setOnClickListener {
            myApplyAdapter.apply {
                data.clear()
                addData(list.filter { it.type=="来访申请" })
                notifyDataSetChanged()
            }
        }
        binding.myRepairCon.setOnClickListener {
            myApplyAdapter.apply {
                data.clear()
                addData(list.filter { it.type=="报修申请" })
                notifyDataSetChanged()
            }
        }
        binding.myPatrolCon.setOnClickListener {
            myApplyAdapter.apply {
                data.clear()
                addData(list.filter { it.type=="巡更申请" })
                notifyDataSetChanged()
            }
        }
    }

}